package ctbu.edu.cn.thydemo.webapi;

import ctbu.edu.cn.thydemo.domain.Student;
import ctbu.edu.cn.thydemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    private StudentService studentService;

    /**
     * 读取的url:/webapi/student/list
     * @return
     */
    @GetMapping("/list")
    public List<Student> getAll(){
        List<Student> students = studentService.findAll();

        return students;
    }

    /**
     * 通过名字进行查询/webapi/student/getList
     * @param name
     * @return
     */
    @GetMapping ("/getList/{name}")
    public List<Student> getList(@PathVariable String name){
        List<Student> student1 = studentService.findBySname(name);
        return student1;
    }

    /**
     * 新增学生的方法/webapi/student/insert
     * @param student
     * @return
     */
    @PostMapping("/insert")
    public Student insert(Student student){
        Student student1 = studentService.insert(student);
        return student1;
    }

    /**
     * 更新学生的方法/webapi/student/update
     * @param student
     * @return
     */
    @PutMapping("/update")
    public Student update(Student student){
        Student student1 = studentService.update(student);
        return student1;
    }
    /**
     * 删除学生的方法/webapi/student/delete
     * @param student
     * @return
     */
//    @DeleteMapping ("/delete")
//    public void delete(Student student){
//        studentService.delete(student);
//
//    }

    /**
     * 读取的url:/webapi/student/get
     * @return
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){
        Student student = studentService.findById(id);
        return student;
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id){
        studentService.delete(id);
    }
}
